<template>
  <page-container>
    <pagination-table
      ref="tabeleIns"
      border="inner"
      :tree-config="{ childrenField: 'children', expandAll: true }"
      :form-data="queryForm"
      :load="loadTableData"
    >
      <template #form="{ submit, reset, bindForm }">
        <el-form :ref="bindForm" inline :model="queryForm">
          <el-form-item prop="name">
            <el-input v-model="queryForm.name" placeholder="角色名"></el-input>
          </el-form-item>
          <el-form-item style="margin-right: 0">
            <el-space>
              <el-button type="primary" @click="submit">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </el-space>
          </el-form-item>
        </el-form>
      </template>
      <template #actions>
        <el-button type="primary" @click="add">新增</el-button>
      </template>

      <vxe-column title="角色名称" field="name"> </vxe-column>
      <vxe-column title="关联权限" field="permission"> </vxe-column>
      <vxe-column title="操作" fixed="right" width="120">
        <template #default="scope">
          <el-space class="tool-color">
            <el-button link type="primary" @click.stop="update(scope.row)"
              >编辑</el-button
            >
            <el-button link block type="danger"> 删除 </el-button>
          </el-space>
        </template>
      </vxe-column>
    </pagination-table>
  </page-container>
</template>

<script lang="ts" setup>
  import { ref, type UnwrapRef } from 'vue';
  import useFloat from '@/hooks/useFloat';
  import { getMockList } from '@/api/common';
  import RowForm from './rowForm.vue';

  const tabeleIns = ref<PagenationTableInstance>();

  const { open, close } = useFloat('page', RowForm);

  const add = () => {
    open({
      title: '新增',
      width: 600,
      props: {
        onConfirm: (form: UnwrapRef<any>) => {
          console.log(form);
        },
        onCancel: close
      }
    });
  };

  const update = (row: any) => {
    open({
      title: '新增',
      width: 600,
      props: {
        row,
        onConfirm: (form: UnwrapRef<any>) => {
          console.log(form);
        },
        onCancel: close
      }
    });
  };

  const queryForm = ref({ name: '' });

  const loadTableData: loadPaginationRequest<
    UnwrapRef<typeof queryForm>
  > = async () => {
    const dataList = await getMockList();
    return Promise.resolve({ dataList });
  };
</script>
